package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.exam.pojo.entity.ExamParticipant;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;

import java.util.List;

/**
 * 考试参与者Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface ExamParticipantMapper extends BaseMapper<ExamParticipant> {

    /**
     * 根据考试ID查询参与者列表
     * 
     * @param examId 考试ID
     * @return 参与者列表
     */
    @Select("SELECT * FROM exam_participant WHERE exam_id = #{examId} ORDER BY create_time DESC")
    List<ExamParticipant> selectByExamId(@Param("examId") String examId);

    /**
     * 根据用户ID查询参与的考试列表
     * 
     * @param userId 用户ID
     * @return 参与的考试列表
     */
    @Select("SELECT * FROM exam_participant WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<ExamParticipant> selectByUserId(@Param("userId") String userId);

    /**
     * 根据考试ID和用户ID查询参与记录
     * 
     * @param examId 考试ID
     * @param userId 用户ID
     * @return 参与记录
     */
    @Select("SELECT * FROM exam_participant WHERE exam_id = #{examId} AND user_id = #{userId}")
    ExamParticipant selectByExamIdAndUserId(@Param("examId") String examId, @Param("userId") String userId);

    /**
     * 分页查询考试参与者
     * 
     * @param page 分页参数
     * @param examId 考试ID
     * @param userName 用户姓名（模糊查询）
     * @param status 状态
     * @return 分页结果
     */
    IPage<ExamParticipant> selectParticipantPage(Page<ExamParticipant> page,
                                                @Param("examId") String examId,
                                                @Param("userName") String userName,
                                                @Param("status") Integer status);

    /**
     * 统计考试参与人数
     * 
     * @param examId 考试ID
     * @param status 状态
     * @return 参与人数
     */
    @Select("SELECT COUNT(*) FROM exam_participant WHERE exam_id = #{examId} AND status = #{status}")
    Integer countByExamIdAndStatus(@Param("examId") String examId, @Param("status") Integer status);

    /**
     * 根据考试ID删除所有参与者
     * 
     * @param examId 考试ID
     * @return 删除行数
     */
    @Delete("DELETE FROM exam_participant WHERE exam_id = #{examId}")
    int deleteByExamId(@Param("examId") String examId);

    /**
     * 批量插入参与者
     * 
     * @param participants 参与者列表
     * @return 插入行数
     */
    int batchInsert(@Param("list") List<ExamParticipant> participants);

    /**
     * 检查用户是否可以参与考试
     * 
     * @param examId 考试ID
     * @param userId 用户ID
     * @return 是否可以参与
     */
    @Select("SELECT COUNT(*) > 0 FROM exam_participant WHERE exam_id = #{examId} AND user_id = #{userId} AND status = 1")
    Boolean canUserParticipate(@Param("examId") String examId, @Param("userId") String userId);

    /**
     * 获取考试的所有参与者用户ID
     * 
     * @param examId 考试ID
     * @return 用户ID列表
     */
    @Select("SELECT user_id FROM exam_participant WHERE exam_id = #{examId} AND status = 1")
    List<String> selectUserIdsByExamId(@Param("examId") String examId);
}
